package com.gugee.tiktok.data.consumer.keyword.core.spider;

import com.xiguaji.boot.retry.RetryableException;
import com.xiguaji.boot.toolkit.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Component
@Slf4j
public class KeywordSpiderClient {
    @Autowired
    RestTemplate restTemplate;

    @Value("${xiguaji.spider.host}")
    String host;

    @Value("${ai.url}")
    String aiUrl;

    @Retryable(value = RetryableException.class, maxAttempts = 10)
    public List<String> getCommentAnalyzed(Map<Integer,String> commentJson) {
        Map<String,Object> body = new HashMap<>();
        body.put("filterstopwords",1);
        body.put("content",commentJson);
        body.put("filtertags","");
        body.put("lang","english");
        List<String> list = new ArrayList<>();
        String bodyJson=JsonUtil.parseJson(body);
        try {
            ResponseEntity<Map> responseEntity = restTemplate.postForEntity(aiUrl + "getsegbulkstrsforeign", bodyJson, Map.class);

            if (responseEntity.getStatusCode() != HttpStatus.OK) {
                log.error("RuntimeException bodyJson={},body.size={},api={},method=POST", bodyJson, body.size(),aiUrl + "getsegbulkstrsforeign");
                throw new RuntimeException(String.format("get ai analyzed error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
            }
            if (responseEntity.getBody() == null) {
                throw new RetryableException("get blogger awemes error, retry times 10. uri:" + aiUrl);
            }
            responseEntity.getBody().forEach((k, v) -> {
                list.addAll((Collection<? extends String>) v);
            });
        }catch (ResourceAccessException e){
//            log.error("##################ResourceAccessException bodyJson={},body.size={},api={},method=POST", bodyJson, body.size(),aiUrl + "getsegbulkstrsforeign");
            log.error("##################ResourceAccessException  ,body.size={},api={},method=POST,msg={}",   body.size(),aiUrl + "getsegbulkstrsforeign",e.getMessage());
            throw e;

        }catch (Exception e){
//            log.error("bodyJson={},body.size={},api={},method=POST", bodyJson, body.size(),aiUrl + "getsegbulkstrsforeign");
            throw e;
        }

        return list;
    }

    @Retryable(value = RetryableException.class, maxAttempts = 10)
    public String getCategories(String title,String comment) {
        Map<String,Object> body = new HashMap<>();
        body.put("title",title);
        body.put("comms",comment);
        ResponseEntity<Map> responseEntity = restTemplate.postForEntity(aiUrl+"getvideoclsfortiktok", JsonUtil.parseJson(body), Map.class);

        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new RuntimeException(String.format("get ai analyzed error. status: %s . msg: %s", responseEntity.getStatusCodeValue(), responseEntity.getBody()));
        }
        if (responseEntity.getBody() == null) {
            throw new RetryableException("get blogger awemes error, retry times 10. uri:" + aiUrl);
        }

        return responseEntity.getBody().get("cls").toString();
    }
}
